"""
Created on 19-Aug-2009

@author: dexen
"""

import os
import sys

import rpyc 
from rpyc.utils import server

import custom_service

from dexen.system import util
from dexen.system import population

temp_dir = os.path.abspath("pyamf_temp")
config_file = "config.py"

class PyAMFService(custom_service.MyService):
    def on_connect(self):
        self.conn = rpyc.connect_by_service("FRONTEND")
        self.mserver = self.conn.root

    def _run_config(self, schema_name):    
        os.chdir(temp_dir)
        sys.path.append(temp_dir)
        population.schema_name = schema_name
        execfile(config_file)
        "The scripts to test correctness"
        print self.mserver.get_script_names(schema_name, "pop_synch")
    
    def has_schema(self, schema_name):
        return self.mserver.has_schema(schema_name)
    
    def set_schema(self, schema_name, zip_data, m_time):
        self.mserver.set_schema(schema_name, zip_data, m_time)
        util.unzip(zip_data, temp_dir)
        self._run_config(schema_name)

def main():
    svr = server.ThreadedServer(PyAMFService)
    svr.start()

if __name__ == "__main__":
    main()
